<?php
class ControllerReportSaleCoupon extends Controller
{
    public function index()
    {
        $this->load->language('report/sale_coupon');

        $this->document->setTitle($this->language->get('heading_title'));

        if (isset($this->request->get['filter_date_start'])) {
            $filter_date_start = $this->request->get['filter_date_start'];
        } else {
            $filter_date_start = '';
        }

        if (isset($this->request->get['filter_date_end'])) {
            $filter_date_end = $this->request->get['filter_date_end'];
        } else {
            $filter_date_end = '';
        }

        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {
            $page = 1;
        }

        $url = '';

        if (isset($this->request->get['filter_date_start'])) {
            $url .= '&filter_date_start=' . $this->request->get['filter_date_start'];
        }

        if (isset($this->request->get['filter_date_end'])) {
            $url .= '&filter_date_end=' . $this->request->get['filter_date_end'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('report/sale_coupon', 'token=' . $this->session->data['token'] . $url, true)
        );

        $this->load->model('report/coupon');

        $data['coupons'] = array();

        $filter_data = array(
            'filter_date_start'    => $filter_date_start,
            'filter_date_end'    => $filter_date_end,
            'start'             => ($page - 1) * $this->config->get('config_limit_admin'),
            'limit'             => $this->config->get('config_limit_admin')
        );

        $coupon_total = $this->model_report_coupon->getTotalCoupons($filter_data);

        $results = $this->model_report_coupon->getCoupons($filter_data);

        
        $expOrders = array();
        $expOrders[] = array(
            iconv("UTF-8", "gbk//IGNORE", "折扣名称"),
            iconv("UTF-8", "gbk//IGNORE", "折扣代码"),
            iconv("UTF-8", "gbk//IGNORE", "订单"),
            iconv("UTF-8", "gbk//IGNORE", '合计')
          );
        foreach ($results as $result) {
            $expOrders[] = array(
              'name'   => iconv("UTF-8", "gbk//IGNORE", $result['name']),
              'code'   => iconv("UTF-8", "gbk//IGNORE", $result['code']),
              'orders' => iconv("UTF-8", "gbk//IGNORE", $result['orders']),
              'total'  => iconv("UTF-8", "gbk//IGNORE", $this->currency->format($result['total'], $this->config->get('config_currency'))),
            );
        }

        if (isset($this->request->get['export']) && ($this->request->get['export']==="export")) {
            $file = fopen(DIR_CACHE."sale_order.csv", "w");
            foreach ($expOrders as $line) {
                fputcsv($file, $line);
            }
            fclose($file);
            $fileName = "sale_order.csv";
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename=' . $fileName);
            header('Content-Transfer-Encoding: binary');
            header('Content-Length: ' . filesize(DIR_CACHE.$fileName));
            readfile(DIR_CACHE.$fileName);
            exit();
        }
        
      

        foreach ($results as $result) {
            $data['coupons'][] = array(
                'name'   => $result['name'],
                'code'   => $result['code'],
                'orders' => $result['orders'],
                'total'  => $this->currency->format($result['total'], $this->config->get('config_currency')),
                'edit'   => $this->url->link('marketing/coupon/edit', 'token=' . $this->session->data['token'] . '&coupon_id=' . $result['coupon_id'] . $url, true)
            );
        }

        $data['heading_title'] = $this->language->get('heading_title');

        $data['text_list'] = $this->language->get('text_list');
        $data['text_no_results'] = $this->language->get('text_no_results');
        $data['text_confirm'] = $this->language->get('text_confirm');

        $data['column_name'] = $this->language->get('column_name');
        $data['column_code'] = $this->language->get('column_code');
        $data['column_orders'] = $this->language->get('column_orders');
        $data['column_total'] = $this->language->get('column_total');
        $data['column_action'] = $this->language->get('column_action');

        $data['entry_date_start'] = $this->language->get('entry_date_start');
        $data['entry_date_end'] = $this->language->get('entry_date_end');

        $data['button_edit'] = $this->language->get('button_edit');
        $data['button_filter'] = $this->language->get('button_filter');

        
        $data['button_export'] = $this->language->get('button_export');
        
      

        $data['token'] = $this->session->data['token'];

        $url = '';

        if (isset($this->request->get['filter_date_start'])) {
            $url .= '&filter_date_start=' . $this->request->get['filter_date_start'];
        }

        if (isset($this->request->get['filter_date_end'])) {
            $url .= '&filter_date_end=' . $this->request->get['filter_date_end'];
        }

        $pagination = new Pagination();
        $pagination->total = $coupon_total;
        $pagination->page = $page;
        $pagination->limit = $this->config->get('config_limit_admin');
        $pagination->url = $this->url->link('report/sale_coupon', 'token=' . $this->session->data['token'] . $url . '&page={page}', true);

        $data['pagination'] = $pagination->render();

        $data['results'] = sprintf($this->language->get('text_pagination'), ($coupon_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($coupon_total - $this->config->get('config_limit_admin'))) ? $coupon_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $coupon_total, ceil($coupon_total / $this->config->get('config_limit_admin')));

        $data['filter_date_start'] = $filter_date_start;
        $data['filter_date_end'] = $filter_date_end;

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('report/sale_coupon', $data));
    }
}
